////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//		File Name		:		CTestingLevelState.h
//
//		Author			:		Kevin Jordan
//
//		Purpose			:		A header for all functions and members of the PowerUp class
//
//	 	Dates			:		Written( March 2, 2011 ) by Kevin Jordan		LastUpdate( March 2, 2011  ) by Kevin Jordan 
//	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "CTestingLevelState.h"
#include "CMainMenuState.h"
#include "SGD Wrappers/CSGD_Direct3D.h"
#include "SGD Wrappers/CSGD_DirectInput.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "VIN_ObjectManager.h"
#include "VIN_EventSystem.h"
#include "CSaveOrLoadState.h"
#include "CGame.h"
#include "CEnemy.h"
#include "CParticle.h"
#include "CPowerUp.h"
#include "CTestingLevelState.h"
#include "CGamePlayState.h"
#include "CHubState.h"
#include "CPickup.h"
#include "CPlatform.h"
#include "VIN_SoundManager.h"
#include "MathLib.h"
#include "VIN_BitMapFont.h"
#include <ctime>
#include "tinyxml/tinyxml.h"
#include "CHubState.h"
#include "VIN_BitMapFont.h"


CTestingLevelState::CTestingLevelState()
{
	ImageID = -1;
	fBucketTE = 0.0f;	
	VIN_EventSystem::GetInstance()->ResgisterClient("KillAll",this);	
	WaveCount = 0;
	fEnemySpawnBucket = 0.0f;
	m_nNumOfEnemies = 0;
	m_nMaxEnemies = 1;
	m_nNumOfDeadEnemies = 0;
	m_fWaveTime = 0.0f;
	m_bNoTimeUpdate = false;	
	m_bInShop = false;
	m_nShopSelection = 0;
	m_bRoundComplete = false;
	m_nRoundCount = 1;
	BombToggle = true;
	Blam = false;
	blambucket = 0;
	m_fEndOfWaveBucket = 0;
	m_bWaveOver = false;

}

CTestingLevelState::~CTestingLevelState()
{
	VIN_EventSystem::GetInstance()->UnregisterClient("KillAll",this);
}

void CTestingLevelState::Enter()
{
	m_bNoTimeUpdate = false;
	m_vWaves.clear();
	InitWaves();

	ImageID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/Graphics/Road.png");
	CPlayer* pPlayer = CHubState::GetInstance()->GetPlayer();
	pPlayer->SetPosX(400);
	pPlayer->SetPosY(300);

	TemplateEnemy = new CMeleeEnemy(1);
	pEmitter = new CEmitter();
	pEmitter->Load("resource/XML/rain2.xml");
	pEmitter->SetPosX(800);
	pEmitter->SetPosY(400);

	VIN_SoundManager::GetInstance()->PlayBGM(BGM_STAGE);
	WaveSet();
	enemy_ME = new CMeleeEnemy();
	m_JCE.SetPosX((float)(rand() % (800)));
	m_JCE.SetPosY((float)(rand() % 150 + 450));
	m_JCE.SetHeight(64);
	m_JCE.SetWidth(64);
	BombToggle = true;

	char buffer[100];
	int fileNumber = 1;

	for( int i = 0; i < 8; i++)
	{
		sprintf_s(buffer, sizeof(buffer), "./resource/Graphics/Shop_Pic%d.png", fileNumber );
		m_nShopImage[i] = CSGD_TextureManager::GetInstance()->LoadTexture(buffer);
		fileNumber++;
	}
}

void CTestingLevelState::Exit()
{
	VIN_SoundManager::GetInstance()->StopAllSounds();

	//delete pEmitter;
	//pEnemy->Release();
	VIN_ObjectManager::GetInstance()->RemoveAllObjects();
	CHubState::GetInstance()->GetPlayer()->ResetPlayer();
	CGamePlayState::GetInstance()->GameOver(false);
	CGamePlayState::GetInstance()->WinGame(false);
	WaveCount = 0;
	m_nRoundCount = 1;
	m_nShopSelection = 0;
	CHubState::GetInstance()->GetPlayer()->SetCurAnimation(0);

}

bool CTestingLevelState::Input()
{
	CGame* pGameState = CGame::GetInstance();
	CSGD_DirectInput* pDI = CSGD_DirectInput::GetInstance();
	if( m_bInShop )
	{
		if( pDI->KeyPressed( DIK_LEFT ) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_LEFT) || 
			CSGD_DirectInput::GetInstance()->JoystickDPadPressed(DIR_LEFT))
		{
			m_nShopSelection--;
			if( m_nShopSelection < 0 )
				m_nShopSelection = 7;
		}

		if( pDI->KeyPressed( DIK_RIGHT )|| CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_RIGHT) || 
			CSGD_DirectInput::GetInstance()->JoystickDPadPressed(DIR_RIGHT) )
		{
			m_nShopSelection++;
			if( m_nShopSelection > 7 )
				m_nShopSelection = 0;
		}

		if( pDI->KeyPressed( DIK_DOWN )|| CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_DOWN) || 
			CSGD_DirectInput::GetInstance()->JoystickDPadPressed(DIR_DOWN) )
		{
			m_nShopSelection+= 4;
			if( m_nShopSelection > 7 )
				m_nShopSelection -= 8;
		}

		if( pDI->KeyPressed( DIK_UP ) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_UP) || 
			CSGD_DirectInput::GetInstance()->JoystickDPadPressed(DIR_UP)  )
		{
			m_nShopSelection -= 4;
			if( m_nShopSelection < 0 )
				m_nShopSelection += 8;
		}

		if( pDI->KeyPressed(DIK_ESCAPE) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(1))
		{
			if( m_bInShop )
			{
				m_bInShop = false;
				m_bNoTimeUpdate = false;
				m_nShopSelection = 0;
			}

		}
		if( pDI->KeyPressed( DIK_RETURN ) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(1) )
		{
			switch( m_nShopSelection )
			{
			case 0:
				{
					if( CHubState::GetInstance()->GetPlayer()->GetCash() >= (CHubState::GetInstance()->GetPlayer()->GetCombat()+1) * 300 && CHubState::GetInstance()->GetPlayer()->GetCombat() <= 3 )
					{
						CHubState::GetInstance()->GetPlayer()->SetCombat( CHubState::GetInstance()->GetPlayer()->GetCombat() + 1 );
						CHubState::GetInstance()->GetPlayer()->SetCash( CHubState::GetInstance()->GetPlayer()->GetCash() - ( CHubState::GetInstance()->GetPlayer()->GetCombat() * 300 ) );
					}
				}
				break;
			case 1:
				{
					if( CHubState::GetInstance()->GetPlayer()->GetCash() >= (CHubState::GetInstance()->GetPlayer()->GetDurability()+1) * 300 && CHubState::GetInstance()->GetPlayer()->GetDurability() <= 3 )
					{
						CHubState::GetInstance()->GetPlayer()->SetDurability( CHubState::GetInstance()->GetPlayer()->GetDurability() + 1 );
						CHubState::GetInstance()->GetPlayer()->SetCash( CHubState::GetInstance()->GetPlayer()->GetCash() - ( CHubState::GetInstance()->GetPlayer()->GetDurability() * 300 ) );

					}

				}
				break;
			case 2:
				{
					if( ( CHubState::GetInstance()->GetPlayer()->GetCash() ) >= (( CHubState::GetInstance()->GetPlayer()->GetSpeed()+1) * 300 ) && CHubState::GetInstance()->GetPlayer()->GetSpeed() <= 3 )
					{

						CHubState::GetInstance()->GetPlayer()->SetSpeed( CHubState::GetInstance()->GetPlayer()->GetSpeed() + 1 );  
						CHubState::GetInstance()->GetPlayer()->SetCash( ( CHubState::GetInstance()->GetPlayer()->GetCash() ) - ( CHubState::GetInstance()->GetPlayer()->GetSpeed() * 300 ) );
					}	
				}
				break;
			case 3:
				{
					if( CHubState::GetInstance()->GetPlayer()->GetCash() >= (CHubState::GetInstance()->GetPlayer()->GetRage()+1) * 300 && CHubState::GetInstance()->GetPlayer()->GetRage() <= 3 )
					{
						CHubState::GetInstance()->GetPlayer()->SetRage( CHubState::GetInstance()->GetPlayer()->GetRage() + 1 );
						CHubState::GetInstance()->GetPlayer()->SetCash( CHubState::GetInstance()->GetPlayer()->GetCash() - ( CHubState::GetInstance()->GetPlayer()->GetRage() * 300 ) );
					}
				}
				break;

			}
		}
	}
	else if( !m_bInShop )
	{
		if(CGamePlayState::GetInstance()->GetWin() == false && CGamePlayState::GetInstance()->GetLose() == false)
		{
			if( CSGD_DirectInput::GetInstance()->KeyPressed( DIK_ESCAPE ) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(1) ) 
				CGamePlayState::GetInstance()->SetPause( !CGamePlayState::GetInstance()->GetPause() );
		}
	}

	return true;
}


void CTestingLevelState::Update(float fElapsedTime)
{
	if( m_bInShop )
	{
		m_bNoTimeUpdate = true;
	}

	m_JCE.Update(fElapsedTime);
	if( CGamePlayState::GetInstance()->GetTrigger() )
	{
		fBucketTE += fElapsedTime;
		if( fBucketTE >= 3.5f )
		{
			CGamePlayState::GetInstance()->SetTrigger(false);
			fBucketTE = 0.0f;
		}
	}

	if( !m_bInShop )
	{
		fEnemySpawnBucket += fElapsedTime;
		if( fEnemySpawnBucket >= 1.5f )
		{
			if( m_nNumOfEnemies < m_nMaxEnemies )
			{
				SpawnEnemies();
				fEnemySpawnBucket = 0.0f;
			}
		}
	}

	if( WaveCount != 0 && m_nNumOfDeadEnemies == m_nMaxEnemies && !m_bWaveOver )
	{
		m_bWaveOver = true;
	}

	if( m_bWaveOver == true )
	{
		m_fEndOfWaveBucket += fElapsedTime;
		if( m_fEndOfWaveBucket >= 1.75f )
		{
			WaveSet();
			m_bWaveOver = false;
			m_fEndOfWaveBucket = 0.0f;
		}
	}
	if(Blam)
	{
		blambucket += fElapsedTime;
		if(blambucket >= 1.0f)
		{
			Blam = false;
			blambucket = 0;
		}
	}

	if((m_JCE.GetPosX() + m_JCE.GetWidth()) > 800)
	{
		m_JCE.SetPosX(700);
	}
	if(m_JCE.GetPosX() - m_JCE.GetWidth() < 0)
	{
		m_JCE.SetPosX(0);
	}
	if(m_JCE.GetPosY() + m_JCE.GetHeight() > 600)
	{
		m_JCE.SetPosY(500);
	}



	VIN_EventSystem::GetInstance()->ProcessEvents();
	VIN_ObjectManager::GetInstance()->CheckCollision();
	VIN_ObjectManager::GetInstance()->UpdateObjects(fElapsedTime);
	pEmitter->Update(fElapsedTime);

	if( !m_bNoTimeUpdate )
	{
		m_fWaveTime -= fElapsedTime;
	}

	if( m_fWaveTime <= 0.0f || CHubState::GetInstance()->GetPlayer()->GetLives() <= 0 )
	{
		CGamePlayState::GetInstance()->GameOver(true);
		CGamePlayState::GetInstance()->CalculateScore( GetWaveCount(), CHubState::GetInstance()->GetPlayer()->GetCash(), (int)GetWaveTime(),  (int)CHubState::GetInstance()->GetPlayer()->GetRageAmt(),  (int)CHubState::GetInstance()->GetPlayer()->GetHP());
		VIN_SoundManager::GetInstance()->StopSound(VIN_SoundManager::GetInstance()->GetAlarm());
		m_bNoTimeUpdate = true;
	}

	else if(m_fWaveTime <= 5.0f)
	{
		VIN_SoundManager::GetInstance()->PlayAlarm();
	}
	else
		VIN_SoundManager::GetInstance()->StopSound(VIN_SoundManager::GetInstance()->GetAlarm());

	if( m_bRoundComplete && m_nRoundCount <= 3)
	{
		ResetRound();
		InitWaves();
		WaveSet();
	}
	else if( m_nRoundCount == 3 && m_nNumOfDeadEnemies == m_nMaxEnemies  && WaveCount == 5)
	{
		m_bInShop = false;
		CGamePlayState::GetInstance()->CalculateScore( GetWaveCount(), CHubState::GetInstance()->GetPlayer()->GetCash(), (int)GetWaveTime(),  (int)CHubState::GetInstance()->GetPlayer()->GetRageAmt(),  (int)CHubState::GetInstance()->GetPlayer()->GetHP());
		CGamePlayState::GetInstance()->WinGame(true);
		m_bNoTimeUpdate = true;
	}
}

void CTestingLevelState::Render()
{
	CSGD_Direct3D* pD3D = CSGD_Direct3D::GetInstance();
	CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();
	VIN_BitMapFont* pBF = VIN_BitMapFont::GetInstance();
	pD3D->Clear(100,100,100);

	pTM->Draw(ImageID, 0,0);
	if(BombToggle)
		m_JCE.Render();
	if(Blam)
		VIN_BitMapFont::GetInstance()->Render("BLAM!",100,400,5,D3DCOLOR_XRGB(255,0,255),GAMEFONT);
	pD3D->GetSprite()->Flush();
	if( !m_bInShop )
	{

		if(CGamePlayState::GetInstance()->GetTrigger())
		{
			//VIN_BitMapFont::GetInstance()->Render("ATTACKED", (int)GetPosX(), (int)GetPosY(),1, D3DCOLOR_ARGB(0, 255, 255, 255), 1);
			VIN_BitMapFont::GetInstance()->Render("Triggered Event", (int)(CHubState::GetInstance()->GetPlayer()->GetPosX() - 55), (int)(CHubState::GetInstance()->GetPlayer()->GetPosY() - 55), 1, D3DCOLOR_ARGB(0,255,0,0), 1);
		}

		/*RECT EmitterPlacement;
		SetRect(&EmitterPlacement, (int)pEmitter->GetPosX(), (int)pEmitter->GetPosY(), int(pEmitter->GetPosX() + 15), int(pEmitter->GetPosY() + 15) );
		pD3D->DrawRect( EmitterPlacement, 255,255,255);*/
		//pEmitter->Render();

		if( CGamePlayState::GetInstance()->GetWin() == false && CGamePlayState::GetInstance()->GetLose() == false )
		{
			CGamePlayState::GetInstance()->RenderHUD();
		}
		pD3D->GetSprite()->Flush();
	}
	else if( m_bInShop )
	{
		if( CGamePlayState::GetInstance()->GetWin() == false && CGamePlayState::GetInstance()->GetLose() == false )
		{
			CGamePlayState::GetInstance()->RenderHUD();
		}
		switch( m_nShopSelection )
		{
		case 0:
			{
				pTM->Draw(m_nShopImage[m_nShopSelection], 0,0, 1.25f, 1.25f );																	
			}
			break;
		case 1:
			{
				pTM->Draw(m_nShopImage[m_nShopSelection], 0,0, 1.25f, 1.25f );	


			}
			break;
		case 2:
			{
				pTM->Draw(m_nShopImage[m_nShopSelection], 0,0, 1.25f, 1.25f );
			}
			break;
		case 3:
			{
				pTM->Draw(m_nShopImage[m_nShopSelection], 0,0, 1.25f, 1.25f );
			}
			break;
		case 4:
			{
				pTM->Draw(m_nShopImage[m_nShopSelection], 0,0, 1.25f, 1.25f );
			}
			break;
		case 5:
			{
				pTM->Draw(m_nShopImage[m_nShopSelection], 0,0, 1.25f, 1.25f );
			}
			break;
		case 6:
			{
				pTM->Draw(m_nShopImage[m_nShopSelection], 0,0, 1.25f, 1.25f );
			}
			break;
		case 7:
			{
				pTM->Draw(m_nShopImage[m_nShopSelection], 0,0, 1.25f, 1.25f );    
			}
			break;

		}
		pD3D->GetSprite()->Flush();
		char bufferCombat[100];
		sprintf_s(bufferCombat, sizeof(bufferCombat), "Combat : %d", CHubState::GetInstance()->GetPlayer()->GetCombat() );

		char bufferDurability[100];
		sprintf_s(bufferDurability, sizeof(bufferDurability), "Durability : %d", CHubState::GetInstance()->GetPlayer()->GetDurability() );

		char bufferSpeed[100];
		sprintf_s(bufferSpeed, sizeof(bufferSpeed), "Speed: %d", CHubState::GetInstance()->GetPlayer()->GetSpeed() );

		char bufferRage[100];
		sprintf_s(bufferRage, sizeof(bufferRage), "Rage: %d", CHubState::GetInstance()->GetPlayer()->GetRage() );

		char bufferCash[100];
		sprintf_s(bufferCash, sizeof(bufferCash), "Cash: $%d", CHubState::GetInstance()->GetPlayer()->GetCash() );

		char bufferCostToNext[100];

		//Combat
		pBF->Render(bufferCombat, 100, 206, 0.50f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);
		if( CHubState::GetInstance()->GetPlayer()->GetCombat() == 0 )
		{
			sprintf_s(bufferCostToNext, sizeof(bufferCostToNext), "Cost: $%d", 300);
			pBF->Render(bufferCostToNext, 75, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);
		}
		else if( CHubState::GetInstance()->GetPlayer()->GetCombat()  > 0 &&  CHubState::GetInstance()->GetPlayer()->GetCombat()  < 4)
		{
			sprintf_s(bufferCostToNext, sizeof(bufferCostToNext), "Cost: $%d", ( ( CHubState::GetInstance()->GetPlayer()->GetCombat() + 1) * 300));
			pBF->Render(bufferCostToNext, 75, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);
		}
		else
		{
			pBF->Render("MAX", 100, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);
		}

		//Durability
		pBF->Render(bufferDurability, 275, 206, 0.50f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);
		if( CHubState::GetInstance()->GetPlayer()->GetDurability() == 0 )
		{
			sprintf_s(bufferCostToNext, sizeof(bufferCostToNext), "Cost: $%d", 300);
			pBF->Render(bufferCostToNext, 250, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);						
		}
		else if( CHubState::GetInstance()->GetPlayer()->GetDurability()  > 0 &&  CHubState::GetInstance()->GetPlayer()->GetDurability()  < 4)
		{
			sprintf_s(bufferCostToNext, sizeof(bufferCostToNext), "Cost: $%d", (  ( CHubState::GetInstance()->GetPlayer()->GetDurability() + 1) * 300));
			pBF->Render(bufferCostToNext, 250, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);				
		}
		else
		{		
			pBF->Render("MAX", 275, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);
		}

		//Speed
		pBF->Render(bufferSpeed, 475, 206, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);						
		if( CHubState::GetInstance()->GetPlayer()->GetSpeed() == 0 )
		{
			sprintf_s(bufferCostToNext, sizeof(bufferCostToNext), "Cost: $%d", 300);
			pBF->Render(bufferCostToNext, 450, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);						
		}
		else if( CHubState::GetInstance()->GetPlayer()->GetSpeed()  > 0 &&  CHubState::GetInstance()->GetPlayer()->GetSpeed()  < 4)
		{
			sprintf_s(bufferCostToNext, sizeof(bufferCostToNext), "Cost: $%d", (  ( CHubState::GetInstance()->GetPlayer()->GetSpeed() + 1) * 300));
			pBF->Render(bufferCostToNext, 450, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);										
		}
		else
		{
			pBF->Render("MAX", 475, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);	
		}

		//Rage
		pBF->Render(bufferRage, 675, 206, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);						
		if( CHubState::GetInstance()->GetPlayer()->GetRage() == 0 )
		{
			sprintf_s(bufferCostToNext, sizeof(bufferCostToNext), "Cost: $%d", 300);
			pBF->Render(bufferCostToNext, 650, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);						

		}
		else if( CHubState::GetInstance()->GetPlayer()->GetRage()  > 0 &&  CHubState::GetInstance()->GetPlayer()->GetRage()  < 4)
		{
			sprintf_s(bufferCostToNext, sizeof(bufferCostToNext), "Cost: $%d", (  ( CHubState::GetInstance()->GetPlayer()->GetRage() + 1) * 300));
			pBF->Render(bufferCostToNext, 650, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);										
		}
		else
		{				
			pBF->Render("MAX", 675, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);
		}

		//Cash display
		pBF->Render(bufferCash, 675, 300, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);
	}
}


CTestingLevelState* CTestingLevelState::GetInstance()
{
	static CTestingLevelState instance;
	return &instance;
}

void CTestingLevelState::HandleEvent(CEvent* pEvent)
{
	if( pEvent->GetEventID() == "DynTrig" )
	{
		if( pEvent->GetParam() == this )
			CGamePlayState::GetInstance()->SetTrigger(true);
	}
	if(pEvent->GetEventID() == "KillAll")
	{
		if(BombToggle)
		{
			VIN_ObjectManager::GetInstance()->KillAll();
			m_nNumOfDeadEnemies = m_nNumOfEnemies;
			Blam = true;
			BombToggle = false;
		}
	}
}

void CTestingLevelState::WaveSet()
{

	if( WaveCount < 5 )
	{	

		//Load in the Wave from the Vector
		WaveInfo tempWave;

		unsigned int Size = m_vWaves.size();

		tempWave.fWaveTime = m_vWaves[(Size - 1)].fWaveTime;
		tempWave.NumOfEnemies = m_vWaves[(Size - 1)].NumOfEnemies;

		m_vWaves.pop_back();
		//Increment the Wave count
		WaveCount++;
		//Set the Max Num of Enemies
		m_nMaxEnemies = tempWave.NumOfEnemies;
		//Reset the Number of dead and  alive enemies
		m_nNumOfEnemies = 0;
		m_nNumOfDeadEnemies = 0;
		m_fWaveTime = tempWave.fWaveTime;


	}
	else if( m_nRoundCount < 3 )
	{
		m_bInShop = true;
		m_bNoTimeUpdate = true;
		m_bRoundComplete = true;
		WaveCount = 0;
		m_nRoundCount++;
	}

}


void CTestingLevelState::SpawnEnemies()
{
	srand(unsigned int(time(0)));
	int decide = rand() % 100 + 1;

	int spawn = rand() % 2;

	CMeleeEnemy* pEnemy;

	if(spawn == 1)
		pEnemy = new CMeleeEnemy(*enemy_ME);
	else
		pEnemy = new CMeleeEnemy(*TemplateEnemy);


	if( IsEven(decide) )
	{
		pEnemy->SetPosX(-100);
		pEnemy->SetPosY(( (float)(rand() % 200 ) + 400));
	}
	else if( !IsEven(decide) )
	{
		pEnemy->SetPosX(900);
		pEnemy->SetPosY((float)(( rand() % 200 ) + 400) );
	}	

	pEnemy->SetWidth(32);
	pEnemy->SetHeight(64);
	pEnemy->SetFlicker(true);
	VIN_ObjectManager::GetInstance()->AddObject(0,pEnemy);
	m_nNumOfEnemies++;
	pEnemy->Release();

}

void CTestingLevelState::InitWaves()
{

	m_nMaxEnemies = 0;
	m_nNumOfDeadEnemies = 0;

	TiXmlDocument doc;
	m_vWaves.clear();

	if( doc.LoadFile("resource/XML/Wave.xml") == false)
		return;

	TiXmlElement* Root = doc.RootElement();
	if( Root == NULL )
		return;

	TiXmlElement* pWave = Root->FirstChildElement("Wave");

	do
	{
		double WaveTime;
		int NumOfEnemies;
		pWave->Attribute("time", &WaveTime);
		pWave->Attribute("Enemies", &NumOfEnemies );
		pWave = pWave->NextSiblingElement("Wave");
		WaveInfo tempWave;
		tempWave.fWaveTime = (float)WaveTime;
		tempWave.NumOfEnemies = NumOfEnemies;
		m_vWaves.push_back(tempWave);
	}while(pWave);

	m_bRoundComplete = false;

}

void CTestingLevelState::ResetRound()
{

	WaveCount = 0;
	fEnemySpawnBucket = 0.0f;
	m_nNumOfEnemies = 0;
	m_nMaxEnemies = 1;
	m_nNumOfDeadEnemies = 0;
	m_fWaveTime = 0.0f;
	m_bNoTimeUpdate = false;
	m_nShopSelection = 0;
	m_bRoundComplete = false;
	if(!BombToggle)
	{
		m_JCE.SetPosX((float)(rand() % 500 + 200));
		m_JCE.SetPosY((float)(rand() % 200 + 300) + m_JCE.GetHeight());
		BombToggle = true;
	}

}